﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using LongNet.Util;
using LongNet.Config;

namespace LongNet.Config
{
    /// <summary>
    /// 配置服务
    /// </summary>
    public static class ConfigService
    {
        private static LongNet.Util.LogManager.Logger logger = LongNet.Util.LogManager.GetCurrentClassLogger();
        #region Properties

        /// <summary>
        /// 
        /// </summary>
        public static LongNetConfigEntity LongNetConfig
        {
            get
            {
                return ConfigManager.LongNetConfig.GetConfig();
            }
        }
     

        /// <summary>
        /// 获取配置备份目录
        /// </summary>
        /// <returns></returns>
        private static DirectoryInfo ConfigBackupDirectory
        {
            get
            {
                string folder = HttpUtil.GetAppSettings("LongNet.ConfigBackupFolder", false, true);

                DirectoryInfo directory = null;

                if (!folder.IsNullOrEmpty())
                {
                    try
                    {
                        directory = new DirectoryInfo(Utils.GetFullPath(folder));

                        if (directory.Exists == false)
                            directory.Create();
                    }
                    catch (Exception ex)
                    {
                        if (logger.IsErrorEnabled)
                            logger.Error(ex, "LongNet.ConfigBackupFolder 配置节点错误！");
                    }
                }

                return directory;
            }
        }

        #endregion

        #region ExtraConfig

        /// <summary>
        /// 编辑额外配置
        /// </summary>
        public static void EditLongNetConfig(string author, double version)
        {
            var config = ConfigManager.LongNetConfig.GetConfig();

            config.Author = author;
            config.Version = version;         

            ConfigManager.LongNetConfig.SaveConfig();
        }

        #endregion


        #region 备份主要配置

        private static void SaveTankAgentConfig(Action<LongNetConfigEntity> action)
        {
            var config = ConfigManager.LongNetConfig.GetConfig();

            action(config);

            ConfigManager.LongNetConfig.SaveConfig();
        }

        /// <summary>
        /// 备份主要配置
        /// </summary>
        public static void BackupTankAgentConfig()
        {
            var directory = ConfigBackupDirectory;

            // 目录不存在
            if (directory.IsNull())
                return;

            ConfigManager.LongNetConfig.BackupConfig(directory.FullName + string.Format("\\LongNet_Backup_{0}.config", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-fff")));
        }

        /// <summary>
        /// 还原主要配置
        /// </summary>
        public static void RestoreTankAgentConfig(string fileName)
        {
            if (fileName.IsNullOrEmpty())
                return;

            var directory = ConfigBackupDirectory;

            // 目录不存在
            if (directory.IsNull())
                return;

            ConfigManager.LongNetConfig.RestoreConfig(directory.FullName + "\\" + fileName);
        }

        /// <summary>
        /// 获取主要配置的备份文件列表
        /// </summary>
        public static IList<string> GetTankAgentConfigBackupFiles()
        {
            var directory = ConfigBackupDirectory;

            // 目录不存在
            if (directory.IsNull())
                return new List<string>();

            var list = (from f in directory.GetFiles("LongNet_Backup_*.config", SearchOption.AllDirectories) orderby f.Name descending select f).ToList();

            int max = 20;
            if (list.Count > max)
            {
                for (int i = max; i < list.Count; ++i)
                {
                    try
                    {
                        list[i].Delete();
                    }
                    catch { }
                }
            }

            return (from f in list select f.Name).Take(max).ToList();
        }

        #endregion

    }
}
